package com.hrm.util;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONValue;
import org.json.simple.parser.ContainerFactory;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 * @author zhuchaoqiang
 * JQgrid 多条件搜索查询条件类
 *
 */
@SuppressWarnings("serial")
public class JQgridSearch extends MyObject{

	private String search;
	private String page;
	private String rows;
	private String sidx;
	private String sord;
	private String filters;
	private String searchField;
	private String searchOper;
	private String searchString;
	
	public JQgridSearch(String search, String page, String rows, String sidx,
			String sord, String filters, String searchField,
			String searchOper, String searchString) {
		super();
		this.search = search;
		this.page = page;
		this.rows = rows;
		this.sidx = sidx;
		this.sord = sord;
		this.filters = filters;
		this.searchField = searchField;
		this.searchOper = searchOper;
		this.searchString = searchString;
	}
	
	public JQgridSearch(){
		super();
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public String getFilters() {
		return filters;
	}

	public void setFilters(String filters) {
		this.filters = filters;
	}

	public String getSearchField() {
		return searchField;
	}

	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	public String getSearchOper() {
		return searchOper;
	}

	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}
	public String getSearchSql(){
		return createSql();
	}
	private String createSql(){
		StringBuffer sBuffer=new StringBuffer();
		//System.out.println(JsonEncoding.valueOf(this.filters));
		
		String jsonText = "{\"first\": 123, \"second\": [4, 5, 6], \"third\": 789}";
		  JSONParser parser = new JSONParser();
		  ContainerFactory containerFactory = new ContainerFactory(){
		    public List creatArrayContainer() {
		      return new LinkedList();
		    }

		    public Map createObjectContainer() {
		      return new LinkedHashMap();
		    }
		                        
		  };
		                
		  try{
		    Map json = (Map)parser.parse(filters, containerFactory);
		    Iterator iter = json.entrySet().iterator();
		    while(iter.hasNext()){
		      Map.Entry entry = (Map.Entry)iter.next();
		      if("groupOp".equals(entry.getKey())){
		    	  sBuffer.append(" "+entry.getValue()+" ");
		      }else if("rules".equals(entry.getKey())){
		    	  for (Object obj : (LinkedList<Object>)entry.getValue()) {
		    		  Map<String, String> ruleMap= (LinkedHashMap<String, String>) obj;
		    		  System.out.println(ruleMap.get("field"));
		    		  System.out.println(ruleMap.get("op"));
		    		  System.out.println(ruleMap.get("data"));
				}
		      }
		      System.out.println(entry.getKey() + "=>" + entry.getValue());
		    }
		                        
		    System.out.println("==toJSONString()==");
		    System.out.println(JSONValue.toJSONString(json));
		  }
		  catch(ParseException pe){
		    System.out.println(pe);
		  }
		
		//分页
		int pageNum=Integer.parseInt(this.page);
		int rowNum=Integer.parseInt(this.rows);
		sBuffer.append(" limit "+(pageNum-1)*rowNum+","+pageNum*rowNum+" ");
		
		//排序
		sBuffer.append(" order by "+sidx+" "+sord+" ");
		
		return sBuffer.toString();
	}
	
	private String op2sql(String op){
		if ("eq".equals(op)) {
			return " = ";
		}
		if ("ne".equals(op)) {
			return " != ";
		}
		if ("nn".equals(op)) {
			return " != null ";
		}
		if ("nu".equals(op)) {
			return " == null ";
		}
		if ("bw".equals(op)) {
			return " >=  ";
		}
		if ("bn".equals(op)) {
			return " <=  ";
		}
		if ("ew".equals(op)) {
			return " <=  ";
		}
		if ("en".equals(op)) {
			return " >=  ";
		}
		if ("cn".equals(op)) {
			return " like '%  ";
		}
		return "";
	}
	public static void main(String[] args){
		new JQgridSearch("true", "1", "20", "id",
				"1", "{\"groupOp\":\"AND\",\"rules\":[{\"field\":\"id\",\"op\":\"eq\",\"data\":\"1\"},{\"field\":\"userName\",\"op\":\"eq\",\"data\":\"1\"}]}",
				"",
				"", "").getSearchSql();
	}
}
